package tech.tuister.cninfo.download.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.http.message.BasicNameValuePair;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class HttpUtils {

    private static final CloseableHttpClient client;

    static {
        List<Header> headers = new ArrayList<>();
        headers.add(new BasicHeader(HttpHeaders.ACCEPT, "application/json, text/javascript, */*; q=0.01"));
        headers.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip"));
        headers.add(new BasicHeader(HttpHeaders.ACCEPT_LANGUAGE, "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"));
        headers.add(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"));
        headers.add(new BasicHeader(HttpHeaders.REFERER, "http://www.cninfo.com.cn/"));
        headers.add(new BasicHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.78"));
        // request.addHeader(new BasicHeader("X-Requested-With", "XMLHttpRequest"));

        client = HttpClients.custom()
                .setDefaultHeaders(headers)
                .build();
    }

    public static String get(String url) throws IOException {
        return get(url, null);
    }

    public static String get(String url, List<NameValuePair> params) throws IOException {

        HttpGet httpGet = new HttpGet(url);
        if (CollectionUtils.isNotEmpty(params)) {
            httpGet.setEntity(new UrlEncodedFormEntity(params));
        }

        return client.execute(httpGet, response -> EntityUtils.toString(response.getEntity()));

    }

    public static String post(String url, List<NameValuePair> params) throws IOException {
        HttpPost httpPost = new HttpPost(url);
        // httpPost.setHeader(HttpHeaders.REFERER,"http://www.cninfo.com.cn/new/disclosure/stock?stockCode=000932&orgId=gssz0000932");
        if (CollectionUtils.isNotEmpty(params)) {
            httpPost.setEntity(new UrlEncodedFormEntity(params));
        }

        return client.execute(httpPost, response -> EntityUtils.toString(response.getEntity()));

    }

    public static String post(String url, Object data) throws IOException {
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setEntity(new StringEntity(JSON.toJSONString(data)));

        return client.execute(httpPost, response -> EntityUtils.toString(response.getEntity()));
    }

    public static List<NameValuePair> buildParams(Object data) {
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(data));

        List<NameValuePair> params = new ArrayList<>(jsonObject.size());
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            if (entry.getValue() == null) {
                continue;
            }
            params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
        }

        return params;
    }
}
